package com.shop.web;

import com.shop.service.UploadImageService;
import com.shop.util.FileUpUtil;
import com.shop.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author : WanS
 * @date : 15:39 2020/11/27
 */
@Slf4j
@RestController
@RequestMapping("qiniu")
public class UploadController {

    @Resource
    UploadImageService uploadImageService;


    @PostMapping(value = "image")
    private String upLoadImage(@RequestParam("file") MultipartFile file) throws IOException {
        File f = new File("d:/image.png");
        file.transferTo(f);
        String url = FileUpUtil.upload(f);
        f.delete();
        return "http://qkfzdu4ki.hn-bkt.clouddn.com/"+url;
//        // 获取文件的名称
//        String fileName = file.getOriginalFilename();
//
//        // 使用工具类根据上传文件生成唯一图片名称
//        String imgName = StringUtil.getRandomImgName(fileName);
//
//        if (!file.isEmpty()) {
//
//            FileInputStream inputStream = (FileInputStream) file.getInputStream();
//
//            String path = uploadImageService.uploadQNImg(inputStream, imgName);
//            System.out.print("七牛云返回的图片链接:" + path);
//            return path;
//        }
//        return "上传失败";
    }

}

